package com.nineclock.document.controller;

import com.nineclock.api.document.AppDocumentControllerApi;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.document.dto.*;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.document.service.DocFileHistoryServcie;
import com.nineclock.document.service.DocFileService;
import com.nineclock.document.service.DocFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AppDocumentController implements AppDocumentControllerApi {

    @Autowired
    private DocFileService docFileService;
    @Autowired
    private DocFolderService folderService;
    @Autowired
    private DocCollaborationsService collaborationsService;
    /**
     *
     * @param parentFoldId  如果前端没有传递, 默认为0, 查询顶级文件夹
     * @return
     */
    @Override
    @GetMapping("/listFolderAndFile")
    public Result<DocumentDto> listFolderAndFile(
            @RequestParam(value = "parentFoldId", defaultValue = "0") Long parentFoldId) {

        DocumentDto documentDto = docFileService.listFolderAndFile(parentFoldId);
        return Result.success(documentDto);

    }


    @Override
    @PostMapping("/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto folderDto) {
        folderService.insertFolder(folderDto);
        return Result.success();
    }


    @Override
    @GetMapping("/getFileByFileId")
    public Result<DocFileDto> getFileByFileId(Long id) {
        DocFileDto fileDto = docFileService.getFileByFileId(id);
        return Result.success(fileDto);
    }


    @Override
    @GetMapping("/pagingCollaborations")
    public Result<PageResult<DocumentUserDTO>> pagingCollaborations(
                @RequestParam(value = "id") Long fileId,
                @RequestParam(value = "page", defaultValue = "1") Integer page,
                @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize) {

        PageResult<DocumentUserDTO> pageResult = docFileService.pagingCollaborations(fileId, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 查询文件历史记录
     *
     * @param fileId  文件id
     * @return
     */
    @Autowired
    private DocFileHistoryServcie docFileHistoryServcie;
    @Override
    @GetMapping("/history/{fileId}")
    public Result<List<DocFileHistoryDto>> history(@PathVariable("fileId") Integer fileId) {
        List<DocFileHistoryDto> docFileHistoryDto = docFileHistoryServcie.QueryHistory(fileId);
        return Result.success(docFileHistoryDto);
    }

    @Override
    @GetMapping("/pagingUsers")
    public Result<List<DocUserCollaborationDTO>> pageUsers(Long id) {
        List<DocUserCollaborationDTO> users = collaborationsService.pagingUsers(id);
        return Result.success(users);
    }

    @Override
    @GetMapping("/pagingFile")
    public Result<PageResult<DocFileDto>> pagingFile(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "keyword", required = false) String keyword) {
        PageResult<DocFileDto> pageResult=docFileService.pagingFileService(page,pageSize,keyword);
        return Result.success(pageResult);
    }
}
